package com.ruoyi.project.mobile.house;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.R;
import com.ruoyi.common.utils.RFactory;
import com.ruoyi.project.system.house.house.enums.HouseDto;
import com.ruoyi.project.system.house.house.service.IHouseService;
import com.ruoyi.project.system.house.img.domain.HouseImg;
import com.ruoyi.project.system.house.img.service.IHouseImgService;
import com.ruoyi.project.system.house.orientation.domain.HouseOrientation;
import com.ruoyi.project.system.house.orientation.service.IHouseOrientationService;
import com.ruoyi.project.system.house.renovation.domain.HouseRenovation;
import com.ruoyi.project.system.house.renovation.service.IHouseRenovationService;
import com.ruoyi.project.system.house.type.domain.HouseType;
import com.ruoyi.project.system.house.type.service.IHouseTypeService;
import com.ruoyi.project.system.member.domain.Member;
import com.ruoyi.project.system.member.enums.MemberIsVip;
import com.ruoyi.project.system.member.service.IMemberService;

/**
 * 功能：
 *
 * @author：F_baisi
 * @create：2020/8/30 11:26
 * @version：2020 Version：1.0
 * @company：小牛科技 Created with IntelliJ IDEA
 */
@RestController
@RequestMapping("/api/house")
public class ApiHouseController {


	@Autowired
	private IHouseService houseService;

	@Autowired
	private IHouseImgService houseImgService;

	@Autowired
	private IHouseOrientationService houseOrientationService;

	@Autowired
	private IHouseRenovationService houseRenovationService;

	@Autowired
	private IHouseTypeService houseTypeService;

	@Autowired
	private IMemberService memberService;

	/**
	 * 查询类型
	 * @param uid
	 * @return
	 */
	@GetMapping("/type")
	public R typeList(@RequestParam String uid){
		List<HouseType> houseTypes = this.houseTypeService.selectHouseTypeList(null);
		return RFactory.generateR(houseTypes);
	}

	/**
	 * 查询朝向列表
	 * @param uid
	 * @return
	 */
	@GetMapping("/orientation")
	public R orientationList(@RequestParam String uid){
		List<HouseOrientation> houseOrientations = this.houseOrientationService.selectHouseOrientationList(null);
		return RFactory.generateR(houseOrientations);
	}

	/**
	 * 查询装修列表
	 * @param uid
	 * @return
	 */
	@GetMapping("/renovation")
	public R renovationList(@RequestParam String uid){
		List<HouseRenovation> houseRenovations = this.houseRenovationService.selectHouseRenovationList(null);
		return RFactory.generateR(houseRenovations);
	}


	/**
	 * 添加房源
	 * @param houseDto
	 * @return
	 */
	@PostMapping
	@Transactional
	public R addHouse(@RequestBody HouseDto houseDto){
		//判断用户是否是联盟商
		Member member = this.memberService.selectMemberById(houseDto.getMemberId());
		if(member==null||member.getIsVip().equals(MemberIsVip.NO.getValue())){
			return R.error("您还不是联盟商");
		}
		houseDto.setDeltag(0);
		houseDto.setStatus(0);
		houseDto.setSort(20);
		houseDto.setType(0);
		houseDto.setCreated(DateUtils.getNowDate());
		this.houseService.insertHouse(houseDto);
		//添加图片信息
		if(houseDto.getHouseImgs()!=null){
			for(HouseImg houseImg:houseDto.getHouseImgs()){
				houseImg.setHouseId(houseDto.getId());
				this.houseImgService.insertHouseImg(houseImg);
			}
		}
		return R.ok();
	}
}
